﻿using Hrm.Core;
using Hrm.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Hrm.Web.Studio.Controllers
{
    public class HomeController : Controller
    {
        IServiceService _serviceService;
        IPortfolioService _portfolioService;
        IBlogService _blogService;
        public HomeController(IServiceService serviceService, IPortfolioService portfolioService, IBlogService blogService)
        {
            this._serviceService = serviceService;
            this._portfolioService = portfolioService;
            this._blogService = blogService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Service()
        {
            var list = this._serviceService.GetList(p => p.IsEnable);
            return View(list);
        }

        public ActionResult Portfolio()
        {
            var list = _portfolioService.GetPortfolios();
            ViewBag.PortfolioTypes = this._portfolioService.GetPortfolioTypes();
            return View(list);
        }

        public ActionResult PortfolioItem(int id)
        {
            var model = this._portfolioService.GetPortfolio(id);
            ViewBag.RelatedPortfolios = this._portfolioService.GetRelatedPortfolios(id);
            return View(model);
        }

        public ActionResult Blog(string tag="")
        {
            PageRequest<BlogPost> request = new PageRequest<BlogPost>();
            request.filter = new BlogPost();
            request.filter.Tags = tag;
            request.start = 0;
            request.length = 10;
            var result = this._blogService.PageBlogPostList(request);
            ViewBag.recordsTotal = result.recordsTotal;
            return View(result.data);
        }

        public JsonResult BlogJson(PageRequest<BlogPost>  request)
        {
            request.start = (request.start-1) * request.length;
            var result = this._blogService.PageBlogPostList(request);
            var jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            return Json(jsonResult);
        }

        public ActionResult BlogItem(int id)
        {
            ViewBag.PreBlog = this._blogService.PreBlogPost(id);
            var result = this._blogService.GetById(id);
            ViewBag.NextBlog = this._blogService.NextBlogPost(id);
            return View(result);
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Contact()
        {
            return View();
        }

        public ActionResult KindleBackgroudTest()
        {
            return View();
        }

        public PartialViewResult PartViewPopularBlogs()
        {
            var PopularBlogPostList = this._blogService.PopularBlogPostList();
            return PartialView("_PopuplarBlogs", PopularBlogPostList);
        }

        public PartialViewResult PartViewTagsCloud()
        {
            var BlogTags = this._blogService.GetAllTag();
            return PartialView("_TagsCloud", BlogTags);
        }
    }
}